﻿package
{
    public class MessageListDS
    {
        public var messageList:Array;
        
		public function MessageListDS()
		{
			messageList = new Array();
		}
		
        public function addToMessageList(ms:MessageDS)
        {
            messageList.push(ms);
        }
        
        public function removeFromMessageList(ms: MessageDS)
        {
            function isSame(theItem:*, index:int, arr:Array):Boolean
			{
				return !(theItem == ms);
			}
			
			messageList = messageList.filter(isSame);			
        }
        
        public function clearMessageList()
        {
            messageList = new Array();
        }
        
        public function getMessages(): Array
        {
			sortOn(timeStampOrder);
            return messageList;
        }
		
		public function sortOn(sorter:Function)
		{
			messageList.sort(sorter);
		}
		
		function timeStampOrder(a:MessageDS, b:MessageDS):Number
		{
			var returnVal:Number = 0;
			
			if (a.getTimeStamp() > b.getTimeStamp())
			{
				returnVal = 1;
			}else if (a.getTimeStamp() < b.getTimeStamp())
			{
				returnVal = -1
			}else
			{
				returnVal = 0;
			}
			
			return returnVal;
		}
    }
} 